package com.jx.mlxg.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.math.BigDecimal;
import java.sql.Date;
import java.time.LocalDateTime;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

/**
 *
 * @TableName order_details
 */
@TableName(value ="order_details")
@Data
public class OrderDetails {

    // 关联字段
    @TableField(exist = false)
    private List<Product> products;

    // 关联字段
    @TableField(exist = false)
    private Addresses receiver;

    /**
     *
     */
    @TableId(value = "detail_id", type = IdType.AUTO)
    private Integer detailId;

    /**
     *
     */
    @TableField(value = "order_id")
    private String orderId;

    /**
     *
     */
    @TableField(value = "product_id")
    private Integer productId;

    /**
     *
     */
    @TableField(value = "quantity")
    private Integer quantity;

    /**
     *
     */
    @TableField(value = "unit_price")
    private BigDecimal unitPrice;

    /**
     *
     */
    @TableField(value = "discount")
    private BigDecimal discount;

    /**
     *
     */
    @TableField(value = "created_at")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdAt;

    /**
     *
     */
    @TableField(value = "updated_at")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime  updatedAt;
    /**
     *
     */
    @TableField(value = "total_money")
    private BigDecimal totalMoney;

    // 添加方法：计算总金额
    public void calculateTotalMoney() {
        if (quantity >= 2) { // 如果数量大于等于2，使用批量价格
            totalMoney = unitPrice.multiply(new BigDecimal(quantity));
        } else {
            // 如果数量小于2，使用单桶价格（假设单桶价格是批量价格 + 40）
            BigDecimal singlePrice = unitPrice.add(new BigDecimal(40));
            totalMoney = singlePrice.multiply(new BigDecimal(quantity));
        }
    }

    @Override
    public boolean equals(Object that) {
        if (this == that) {
            return true;
        }
        if (that == null) {
            return false;
        }
        if (getClass() != that.getClass()) {
            return false;
        }
        OrderDetails other = (OrderDetails) that;
        return (this.getDetailId() == null ? other.getDetailId() == null : this.getDetailId().equals(other.getDetailId()))
                && (this.getOrderId() == null ? other.getOrderId() == null : this.getOrderId().equals(other.getOrderId()))
                && (this.getProductId() == null ? other.getProductId() == null : this.getProductId().equals(other.getProductId()))
                && (this.getQuantity() == null ? other.getQuantity() == null : this.getQuantity().equals(other.getQuantity()))
                && (this.getUnitPrice() == null ? other.getUnitPrice() == null : this.getUnitPrice().equals(other.getUnitPrice()))
                && (this.getDiscount() == null ? other.getDiscount() == null : this.getDiscount().equals(other.getDiscount()))
                && (this.getCreatedAt() == null ? other.getCreatedAt() == null : this.getCreatedAt().equals(other.getCreatedAt()))
                && (this.getUpdatedAt() == null ? other.getUpdatedAt() == null : this.getUpdatedAt().equals(other.getUpdatedAt()))
                && (this.getTotalMoney() == null ? other.getTotalMoney() == null : this.getTotalMoney().equals(other.getTotalMoney()));
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((getDetailId() == null) ? 0 : getDetailId().hashCode());
        result = prime * result + ((getOrderId() == null) ? 0 : getOrderId().hashCode());
        result = prime * result + ((getProductId() == null) ? 0 : getProductId().hashCode());
        result = prime * result + ((getQuantity() == null) ? 0 : getQuantity().hashCode());
        result = prime * result + ((getUnitPrice() == null) ? 0 : getUnitPrice().hashCode());
        result = prime * result + ((getDiscount() == null) ? 0 : getDiscount().hashCode());
        result = prime * result + ((getCreatedAt() == null) ? 0 : getCreatedAt().hashCode());
        result = prime * result + ((getUpdatedAt() == null) ? 0 : getUpdatedAt().hashCode());
        result = prime * result + ((getTotalMoney() == null) ? 0 : getTotalMoney().hashCode());
        return result;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", detail_id=").append(detailId);
        sb.append(", order_id=").append(orderId);
        sb.append(", product_id=").append(productId);
        sb.append(", quantity=").append(quantity);
        sb.append(", unit_price=").append(unitPrice);
        sb.append(", discount=").append(discount);
        sb.append(", created_at=").append(createdAt);
        sb.append(", updated_at=").append(updatedAt);
        sb.append(", total_money=").append(totalMoney);
        sb.append("]");
        return sb.toString();
    }
}